import { FormControl } from "@web-design/ui"
import { AntdModalProps } from "@web-design/ui/es/useAntdModal"
import { Form, Input, Modal, Radio, Select, message } from "antd"
import React from "react"
import { saveCrontab } from "../../../../../api/task"
const SaveParams = ({ form, closeModal, payload, confirmLoading, refresh, modalProps }: AntdModalProps) => {
    const onOk = async () => {
        confirmLoading(true)
        try {
            const data = await form.validateFields()
            await saveCrontab(data)
            closeModal()
            message.success('保存成功')
            refresh()
        } catch (error) {
            console.error(error)
        }
        confirmLoading(false)


    }
    //    const scriptType = Form.useWatch('scriptType',form);
    const type = Form.useWatch('type', form);
    return <Modal title='编辑' width={800}  {...modalProps} onOk={onOk} >
        <FormControl
            labelWidth={120}
            initialValues={{
                status: 0,
                name: '文本生成图片',
                type: 'task_list',
                params: JSON.stringify({
                    "prompt": '',
                    "negative_prompt": '',
                    "seed": -1,
                    "save_images": true,
                    "steps": 30
                }, null, 4)
            }}
            form={form}>
            <FormControl.Panel
                items={[{
                    name: 'id',
                    hidden: true,

                }, {
                    name: 'name',
                    rules: [{ required: true }],
                    label: '定时器名称'
                }, {
                    name: 'cron',
                    rules: [{ required: true }],

                    label: '定时规则'
                }, {
                    name: 'type',
                    label: '定时器类型',
                    rules: [{ required: true }],

                    children: <Select
                        options={[{
                            label: '任务列表',
                            value: 'task_list'
                        }, {
                            label: '执行请求',

                            value: 'request'
                        }, {
                            label: '自定义脚本',


                            value: 'custom_script'
                        }]}
                    />
                },
                {
                    name: 'scriptType',
                    label: '脚本类型',
                    hidden: type != 'custom_script',
                    children: <Select
                        options={[{
                            label: 'NodeJS',
                            value: 'node'
                        }, {
                            label: 'Python',
                            value: 'python'
                        }, {
                            label: 'Shell',
                            value: 'shell'
                        }]}
                    />
                },

                {
                    name: 'scriptBody',
                    label: '脚本',
                    // hidden:type!='custom_script',
                    rules: [{ required: true }],
                    children: <Input.TextArea rows={10} />
                }, {
                    label: '请求地址',
                    hidden: type != 'request',
                    name: 'requestUrl'
                }, {
                    label: '请求类型',
                    hidden: type != 'request',
                    name: 'requestMethod',
                    children: <Select
                        options={[{
                            label: 'POST',
                            value: 'POST'
                        }, {
                            label: 'GET',
                            value: 'GET'
                        }]}
                    />
                }, {
                    label: '请求参数',
                    hidden: type != 'request',
                    name: 'requestBody',
                    children: <Input.TextArea />
                }
                ]}
            />
        </FormControl>
    </Modal>
}
export default SaveParams